package script.prefabScript
{
	import eventUtil.EventCenter;
	
	import laya.components.Script;
	import laya.events.Event;
	import laya.media.SoundManager;
	import laya.ui.Box;
	import laya.ui.Image;
	import laya.ui.Label;
	import laya.utils.Browser;
	
	import model.ConstantModel;
	import model.HttpRequestUtil;
	
	public class DialogBoxCell extends Script
	{
		/** @prop {name:translateTxt,tips:"翻译",type:String}*/
		public var translateTxt:String = "";
		
		/** @prop {name:soundTxt,tips:"朗读字符串（解决中文汉语拼音百度语音不准问题)",type:String}*/
		public var soundTxt:String = "";
		
		/** @prop {name:soundSrc,tips:"元音",type:String}*/
		public var soundSrc:String = "";
		
		
		/** @prop {name:languageType,tips:"语音类型",type:int,0:英语 1：中文}*/
		public var languageType:int = 0;

		private var frame:Image;
		private var lbl:Label;
		
		/** @prop {name:per,tips:"配音",type:int}*/
		private var per:int = 0;//配音  0 成人男  1 成人女   2 儿童男   3 儿童女 
		public function DialogBoxCell()
		{
			super();
		}
		
		override public function onStart():void
		{
			var box:Box = this.owner as Box;
			
			frame = box.getChildByName("frame") as Image;
			lbl = box.getChildByName("txt") as Label;
			
			box.on(Event.CLICK,this,onReadTxt);
			frame.visible = false;

			
		}
		
		private function onReadTxt():void
		{
			
//			var postdata:String = "text=" + lbl.text + "&language=en"  + "&per=xiaoyun";
//			HttpRequestUtil.instance.Request(HttpRequestUtil.httpUrl + HttpRequestUtil.sendCreateVoice,this,onAddBack,postdata,"post");
//			return;
			
			frame.visible = true;
			//Browser.window.textToAudio(lbl.text, languageType == 0?"en":"zh");
			if(soundSrc != "")
				SoundManager.playSound(soundSrc);
			else if(soundTxt != "")
				Browser.window.textToAudio(soundTxt, "zh",ConstantModel.PER_YIN[per]);
			else
				Browser.window.textToAudio(lbl.text, "zh",ConstantModel.PER_YIN[per]);

			Laya.timer.once(2000,this,function(){
				
				frame.visible = false;
			});
			
			EventCenter.instance.event(EventCenter.SHOW_TRANSLATE_TEXT,translateTxt);

		}
		
		private function onAddBack(data:*):void
		{
			
			var testAudio = new Audio();
			testAudio.src = '';
			testAudio.muted = false
			testAudio.play();
				
		}
		
		
	}
}